<template>
	<view class="uni-goods-nav">
		<!-- 底部占位 -->
		<view class="uni-tab__seat" />
		<view class="uni-tab__cart-box flex">
			<view class="flex uni-tab__cart-sub-left">
				<view v-for="(item,index) in options" :key="index" class="flex uni-tab__cart-button-left uni-tab__shop-cart" @click="onClick(index,item)">
					<view class="uni-tab__icon" style="margin-bottom: 3px;">
						<uni-icons :type="item.icon" size="20" color="#646566"></uni-icons>
						<!-- <image class="image" :src="item.icon" mode="widthFix" /> -->
					</view>
					<text class="uni-tab__text">{{ item.text }}</text>
					<view class="flex uni-tab__dot-box">
						<text v-if="item.info" :class="{ 'uni-tab__dots': item.info > 9 }" class="uni-tab__dot ">{{ item.info }}</text>
					</view>
					<button v-if="item.text=='客服'" style="position: absolute;opacity:0" open-type="contact"></button>
				</view>
			</view>
			<view :class="{'uni-tab__right':fill}" class="flex uni-tab__cart-sub-right ">
				<view v-for="(item,index) in buttonGroup" :key="index">
					<button  v-if="index==0" :style="{backgroundColor:item.backgroundColor,color:item.color}"
					 class="flex uni-tab__cart-button-right" @click="buttonClick(index,item)"><text class="uni-tab__cart-button-right-text">{{ item.text }}</text></button>
					<button  v-if="index==1&&hasLogin==true" :style="{backgroundColor:item.backgroundColor,color:item.color}"
					 class="flex uni-tab__cart-button-right" @click="buttonClick(index,item)"><text class="uni-tab__cart-button-right-text">{{ item.text }}</text></button>
					<button  v-if="index==1&&hasLogin==false" :style="{backgroundColor:item.backgroundColor,color:item.color}"
					 class="flex uni-tab__cart-button-right" @tap="oauth('weixin')" open-type="getUserInfo" @getuserinfo="mpGetUserInfo"><text class="uni-tab__cart-button-right-text">{{ item.text }}</text></button>

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniIcons from '../uni-icons/uni-icons.vue'
	import { wxLogin,getUserInfo} from '@/common/api/user.js'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	/**
	 * GoodsNav 商品导航
	 * @description 商品加入购物车、立即购买等
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=865
	 * @property {Array} options 组件参数
	 * @property {Array} buttonGroup 组件按钮组参数
	 * @property {Boolean} fill = [true | false] 组件按钮组参数
	 * @event {Function} click 左侧点击事件
	 * @event {Function} buttonClick 右侧按钮组点击事件
	 * @example <uni-goods-nav :fill="true"  options="" buttonGroup="buttonGroup"  @click="" @buttonClick="" />
	 */
	export default {
		name: 'UniGoodsNav',
		components: {
			uniIcons
		},
		props: {
			options: {
				type: Array,
				default () {
					return [{
						icon: 'shop',
						text: '店铺'
					}, {
						icon: 'cart',
						text: '购物车'
					}]
				}
			},
			buttonGroup: {
				type: Array,
				default () {
					return [{
							text: '加入购物车',
							backgroundColor: '#ffa200',
							color: '#fff'
						},
						{
							text: '立即购买',
							backgroundColor: '#ff0000',
							color: '#fff'
						}
					]
				}
			},
			fill: {
				type: Boolean,
				default: false
			}
		},
		computed: {
			...mapState(['hasLogin','userId'])
		},
		methods: {
			...mapMutations(['login', 'setUserInfo']),
			onClick(index, item) {
				this.$emit('click', {
					index,
					content: item,

				})
			},
			buttonClick(index, item) {
				if (uni.report) {
					uni.report(item.text, item.text)
				}
				this.$emit('buttonClick', {
					index,
					content: item
				})
			},
			setCartInfo(num) {
				this.options[2].info = num
			},
			mpGetUserInfo(result) {
				if(this.userId!=''){
					if (result.detail.errMsg !== 'getUserInfo:ok') {
						uni.showModal({
							title: '获取用户信息失败',
							content: '错误原因' + result.detail.errMsg,
							showCancel: false
						});
						return;
					}
					const data = {
						uid: this.userId,
						userInfo: result.detail.userInfo
					}
					updateUser(data).then(res=>{
						
					})
					uni.setStorageSync('userInfo', result.detail.userInfo)
					this.setUserInfo(result.detail.userInfo)
				}
			},
			oauth(v) {
				var that = this;
				uni.login({
					provider: v,
					success: function(loginRes) {
						const data = {
							code: loginRes.code
						}
						wxLogin(data).then(res=>{
							that.$api.msg(res.msg);
							if (res.code == 0) {
								const data2 = {
									uid: res.uid,
									field: ['mobile']
								}
								getUserInfo(data2).then(res2=>{
									if (res2.code == 0) {
										if (res2.userInfo.mobile == undefined) {
											res.mobile = ''
										} else {
											res.mobile = res2.userInfo.mobile
										}
										uni.setStorageSync('userId', res.uid)
										uni.setStorageSync("userName",res.mobile);
										uni.setStorageSync('uniIdToken', res.token)
										uni.setStorageSync('uni_id_token_expired', res.tokenExpired)
										that.login(res)
									}
								})
							}
						})
					}
				});
			},
		}
	}
</script>

<style scoped>
	.flex {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.uni-goods-nav {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		flex-direction: row;
	}

	.uni-tab__cart-box {
		flex: 1;
		height: 50px;
		background-color: #fff;
		z-index: 900;
	}

	.uni-tab__cart-sub-left {
		padding: 0 5px;
	}

	.uni-tab__cart-sub-right {
		flex: 1;
	}

	.uni-tab__right {
		margin: 5px 0;
		margin-right: 10px;
		border-radius: 100px;
		overflow: hidden;
	}

	.uni-tab__cart-button-left {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		/* flex: 1;
 */
		position: relative;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		margin: 0 10px;
	}

	.uni-tab__icon {
		width: 18px;
		height: 18px;
	}

	.image {
		width: 18px;
		height: 18px;
	}

	.uni-tab__text {
		margin-top: 6px;
		font-size: 24rpx;
		color: #646566;
	}

	.uni-tab__cart-button-right {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: column;
		/* #endif */
		flex: 1;
		justify-content: center;
		align-items: center;
		border-radius: 20px;
		margin-left: 10rpx;
		height: 40px;
		width: 215rpx;
	}

	.uni-tab__cart-button-right-text {
		font-size: 28rpx;
		color: #fff;
	}

	.uni-tab__cart-button-right:active {
		opacity: 0.7;
	}

	.uni-tab__dot-box {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: column;
		/* #endif */
		position: absolute;
		right: -2px;
		top: 2px;
		justify-content: center;
		align-items: center;
		/* width: 0;
 */
		/* height: 0;
 */
	}

	.uni-tab__dot {
		/* width: 30rpx;
 */
		/* height: 30rpx;
 */
		padding: 0 4px;
		line-height: 15px;
		color: #ffffff;
		text-align: center;
		font-size: 12px;
		background-color: #ff0000;
		border-radius: 15px;
	}

	.uni-tab__dots {
		padding: 0 4px;
		/* width: auto;
 */
		border-radius: 15px;
	}

	.uni-tab__color-y {
		background-color: #ffa200;
	}

	.uni-tab__color-r {
		background-color: #ff0000;
	}
</style>
